package zld369504.tw.m_team;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import okhttp3.Cookie;

@DatabaseTable(tableName = "tb_cookie")
public class LocalCookieStore {

    @DatabaseField(generatedId = true)
    private long id;
    @DatabaseField(columnName = "name")
    private String name;
    @DatabaseField(columnName = "value")
    private String value;
    @DatabaseField(columnName = "expiresAt")
    private long expiresAt;
    @DatabaseField(columnName = "domain")
    private String domain;
    @DatabaseField(columnName = "path")
    private String path;
    @DatabaseField(columnName = "secure")
    private boolean secure;
    @DatabaseField(columnName = "httpOnly")
    private boolean httpOnly;
    @DatabaseField(columnName = "persistent")
    private boolean persistent; // True if 'expires' or 'max-age' is present.
    @DatabaseField(columnName = "hostOnly")
    private boolean hostOnly;

    public static final LocalCookieStore okhttpCookie2LocalCookieStore(Cookie cookie) {
        LocalCookieStore store = new LocalCookieStore();
        store.domain = cookie.domain();
        store.expiresAt = cookie.expiresAt();
        store.hostOnly = cookie.hostOnly();
        store.httpOnly = cookie.httpOnly();
        store.name = cookie.name();
        store.value = cookie.value();
        store.path = cookie.path();
        store.persistent = cookie.persistent();
        store.secure = cookie.secure();
        return store;
    }

    public static final Cookie localCookieStore2OkhttpCookie(LocalCookieStore store) {
        Cookie.Builder builder = new Cookie.Builder();

        builder.name(store.name);
        builder.value(store.value);
        builder.expiresAt(store.expiresAt);
        if (store.hostOnly) {
            builder.hostOnlyDomain(store.domain);
        } else {
            builder.domain(store.domain);
        }
        builder.path(store.path);
        if (store.secure) {
            builder.secure();
        }
        if (store.httpOnly) {
            builder.httpOnly();
        }
        return builder.build();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public long getExpiresAt() {
        return expiresAt;
    }

    public void setExpiresAt(long expiresAt) {
        this.expiresAt = expiresAt;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isSecure() {
        return secure;
    }

    public void setSecure(boolean secure) {
        this.secure = secure;
    }

    public boolean isHttpOnly() {
        return httpOnly;
    }

    public void setHttpOnly(boolean httpOnly) {
        this.httpOnly = httpOnly;
    }

    public boolean isPersistent() {
        return persistent;
    }

    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

    public boolean isHostOnly() {
        return hostOnly;
    }

    public void setHostOnly(boolean hostOnly) {
        this.hostOnly = hostOnly;
    }
}
